<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ui{
            margin-top: 50px;
        }

        li{
            width: 300px;
            padding: 5px;
            background-color: rgb(245,209,243);
            color: red;
            font-size: 14px;
            margin: 15px 0;
        }
        li a {
            float: right;
        }
    </style>
</head>
<body>
    <textarea name="" id="" cols="30" rows="10"></textarea>
    <button>发布</button>
    <ul>

    </ul>
    <script>
        
        var ul = document.querySelector('ul')
        var text = document.querySelector('textarea');
        var btn = document.querySelector('button');
        btn.onclick =function(){
           if(text.value == ''){
               alert('请输入内容');
               return false;
           }else{
                //1.创建元素
                    var li = document.createElement('li');
                    var btn1 = document.createElement('button');
                //2.添加元素
                    //ul.appendChild(li);//添加到后面
                    ul.insertBefore(li,ul.children[0]);//添加到前面s
                    li.innerHTML = text.value + '<a herf="javascript:;" >删除</a>';
                    text.value = '';    
                    //3.删除元素  删除的是当前链接的Li
                    var as =document.querySelectorAll('a');
                    for(var i =0;i< as.length;i++){
                       as[i].onclick =function(){
                           //删除的是li 当前a所在的li this.parntNode
                           ul.removeChild(this.parentNode);
                       }
                    }

                    }
           }
        
    </script>
</body>
</html>